package com.zhao.common.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.zhao.common.support.Response;

@Component
public class UploadUtil {
	/**
	 * @param file
	 * @param name
	 * @return
	 */
	public Response uloadFile(MultipartFile file, String name) {
		if (StringUtils.isEmpty(name)) {
			name = file.getOriginalFilename();
		}
		if (!file.isEmpty()) {
			try {
				byte[] bytes = file.getBytes();
				BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
				stream.write(bytes);
				stream.close();
				return Response.backSuccess();
			} catch (Exception e) {
				e.printStackTrace();
				return Response.backException("上传失败");
			}
		} else {
			return Response.backException("空文件");
		}
	}
}
